package com.younion.action.consumer.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IAppMarkService;
import com.younion.common.constants.SysConstants;
import com.younion.model.AppMarkResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.AppMarkVO;

public class AppMarkConsumer extends AbstractRequestConsumer {
	private IAppMarkService appMarkService = null;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getApp(request, response);
	}

	public String getApp(HttpServletRequest request,
			HttpServletResponse response) {
		List<AppMarkResult> appMarkResultList = new ArrayList<AppMarkResult>();
		List<AppMarkVO> appMarkVOList = null;
		Map paramMap = new HashMap();
		ResultObject resultObject = new ResultObject();
		try {
			appMarkVOList = (List<AppMarkVO>) appMarkService.getAll();
			if (appMarkVOList != null && appMarkVOList.size() > 0) {
				String img_url_pre = ConfigHelper.fastGetConfig("interface",
						"img_url_pre", "http://localhost:8080/younonmember/");
				for (AppMarkVO appMarkVO : appMarkVOList) {
					AppMarkResult appMarkResult = new AppMarkResult();
					appMarkResult.setId(appMarkVO.getId().toString());
					appMarkResult.setAppName(appMarkVO.getName());
					appMarkResult.setIntroduction(appMarkVO.getIntroduction());
					appMarkResult.setPic(img_url_pre
							+ SysConstants.APP_MARK_IMG_URL + "/"
							+ appMarkVO.getId() + "/" + appMarkVO.getPic());
					appMarkResult.setUrl(appMarkVO.getUrl());
					appMarkResultList.add(appMarkResult);
				}
			}
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		resultObject.setResult(appMarkResultList);
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setAppMarkService(IAppMarkService appMarkService) {
		this.appMarkService = appMarkService;
	}

}
